package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.userEmp.SysUnderlingDto;
import com.zlm.common.entity.SysUnderlingDO;
import com.zlm.common.enums.EducationEnum;
import com.zlm.common.enums.JobHuntingStatusEnum;
import com.zlm.common.enums.MarriedEnum;
import com.zlm.common.enums.SexEnum;
import com.zlm.common.util.PinyinUtils;
import com.zlm.user.emp.dao.SysEmpDao;
import com.zlm.user.emp.dao.SysUnderlingDao;
import com.zlm.common.dto.userEmp.SysEmpDto ;
import com.zlm.user.emp.service.SysUnderlingService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/5/5 17:13
 */
@Service
public class SysUnderlingServiceImpl extends AbstractBaseService<SysUnderlingDao, SysUnderlingDO> implements SysUnderlingService {
    @Autowired
    @Override
    public void setBaseDao(SysUnderlingDao sysUnderlingDao) {
        this.baseDao = sysUnderlingDao;
    }
    @Autowired
    private SysEmpDao sysEmpDao;

    @Override
    public String editLocalPartner(Integer operationCode, Integer partId, Integer empId) {
        if (operationCode.intValue() == 1) {
            //添加职业顾问
            int count = baseDao.countLocalPartner(partId,empId);
            if (count > 0) {
                return CodeConstants.CODE_10010;
            }
            //获取当前用户的基本信息
            SysEmpDto emp = sysEmpDao.queryEmpById(empId);
            //创建SysUnderling对象
            SysUnderlingDO underling = new SysUnderlingDO();
            underling.setPartId(partId);
            underling.setEmpId(empId);
            underling.setMobile(emp.getMobile());
            underling.setUnderName(StringUtils.isBlank(emp.getEmpName()) ? UserConstants.USER_DEFAULT_NAME : emp.getEmpName());
            underling.setIdNo(emp.getIdNo());
            //处理名字首字母
            underling.setFirstLetter(PinyinUtils.getFirstLetter(emp.getEmpName()));
            baseDao.saveLocalPartner(underling);
        }else if (operationCode.intValue() == 0) {
            baseDao.deleteLocalPartner(partId,empId);
        }else {
            return CodeConstants.CODE_PARAMS;
        }
        return CodeConstants.CODE_SUCCESS;
    }

    @Override
    public List<Integer> queryUnderlingList(Integer empId) {
        return baseDao.queryUnderlingList(empId);
    }

    @Override
    public SysUnderlingDto querySingleUnderling(Integer empId) {
        SysUnderlingDto underling = baseDao.querySingleUnderling(empId);
        underling.setSexName(SexEnum.getNameByCode(underling.getSex()));
        underling.setMarriedName(MarriedEnum.getNameByCode(underling.getMarried()));
        underling.setIsWorkName(JobHuntingStatusEnum.getNameByCode(underling.getIsWork()));
        underling.setEducationName(EducationEnum.getNameByCode(underling.getEducation()));
        return underling;
    }
}
